<template>
  <div>
    <el-button @click="visibility = true">新增</el-button>
    <el-dialog :visible.sync="visibility">
      <!--    usernameFk: "2016206666",-->
      <!--    nickname: "马钰雯",-->
      <!--    profession: "architect",-->
      <!--    goldCoin: 66,-->
      <!--    silverCoin: 88,-->
      <!--    playerId: 84,-->
      <el-form ref="play" :model="play" :rules="rules" label-position="right"
               label-width="auto">

        <el-form-item prop="usernameFk" label="用户名：">
          <el-input v-model.trim="play.usernameFk">
          </el-input>
        </el-form-item>
        <el-form-item prop="nickname" label="昵称：">
          <el-input v-model.trim="play.nickname">
          </el-input>
        </el-form-item>
        <el-form-item prop="profession" label="职业：">
<!--          <el-input v-model.trim="play.profession">-->
<!--          </el-input>-->
          <el-select v-model="play.profession">
            <el-option v-for="e in $store.state.professionTypes" :key="e.index" :value="e.value" :label="e.text">

            </el-option>

          </el-select>
        </el-form-item>
        <el-form-item prop="username" label="金币值：">
          <el-input v-model.number="play.goldCoin">
          </el-input>
        </el-form-item>
        <el-form-item prop="username" label="银币值：">
          <el-input v-model.number="play.silverCoin">
          </el-input>
        </el-form-item>


        <el-form-item>
          <el-button type="primary" icon="el-icon-edit"
                     @click="submitplay()">提交
          </el-button>
          <el-button type="warning" icon="el-icon-remove"
                     @click="()=>$refs.play.resetFields()"
          >重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

  </div>
</template>

<script>
// import New from './components/New.vue';
export default {
  name: "InsertPlayer",
  components: {
    // New,Old,
  },
  props: {
    sampleP: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      sampleD: "value",
      play: {
        usernameFk: "2016206666",
        nickname: "马钰雯",
        profession: "architect",
        goldCoin: 66,
        silverCoin: 88,
        playerId: 84,
        playerCode: "8112246",
        version: 0
      },
      visibility: false,
      // rules: {
      //         username: [
      //           { required: true, message: "请输入账号", trigger: "blur" },
      //           {
      //             pattern: /^\w+$/,
      //             message: "请输入字母/数字/下划线",
      //             trigger: ["blur", "change"],
      //           },
      //           { validator: this.validateItem, trigger: "blur" },
      //         ],
      //         password: [{ required: true, message: "请输入密码", trigger: "change" }],
      //       },
      rules: {
        usernameFk: [
          { validator: this.valiUsernameFk, trigger: "blur" }
        ]


      }
    };
  },
  computed: {
    // sampleC() {
    //     return this.sampleD+'--';
    // }
  },
  created() {
  },
  mounted() {
  },
  methods: {
    sampleM() {
    },
    valiUsernameFk(rules, value, callback) {
      if (value.length < 5 || value.length > 10) {
        callback(new Error("长度必须在5-10之间"));
        return;
      }
      if (!value.startWith("2016")) {
        callback(new Error("学号必须以2016开头"));
        return;
      }
      if (!/^\d{5,10}$/.test(value)) {
        callback(new Error("不符合全部是数字"));
      }
      callback();

    },
    submitplay() {
      this.$http({
        method: "post",
        url: "/ceto/player/insert",
        data: this.play
      }).then(
        (res) => {
          //this.$message.warning(JSON.stringify(res.data));
          if(res.data.playerId){
            this.visibility=false;
            this.$emit("onSubmit",1);
          }
        },
        (error) => {
          console.log("request error : " + error.response.status);
        }
      );
    }
  },
  watch: {
    // sampleC(newValue, oldValue) {
    //
    // }
  }
};
</script>

<!--<style scoped>-->
<style lang="scss" scoped></style>
